home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_503 / pcq / pcq12a.lzh / Examples / CheckDisk.p < prev    next >
Text File  |  1991-04-16  |  5KB  |  195 lines

  1. Program CheckDisk;
  2.  
  3. {$I "Include:Utils/Break.i"}
  4. {$I "Include:Libraries/DOS.i"}
  5. {$I "Include:Utils/Parameters.i"}
  6. {$I "Include:Utils/StringLib.i"}
  7.  
  8. {$O-}
  9.  
  10. type
  11. {
  12.     When I'm searching through a directory for files matching the
  13. pattern, I also come across directories.  When I do I keep them in a
  14. linked stack, using the record below, then print all the subdirectories
  15. after I'm finished with the current directory.  That way the files from
  16. the subdirectory aren't printed in the middle of files from the current
  17. directory.
  18. }
  19.     DirRec = record
  20.     Previous : ^DirRec;
  21.     Name : Array [0..109] of Char;
  22.     end;
  23.     DirRecPtr = ^DirRec;
  24.  
  25. var
  26.     FullPath : String;
  27.     Directory : String;
  28.     TestName : String;
  29.     Errors   : Integer;
  30.  
  31. Function DirEnded(FName : String) : Boolean;
  32. {
  33.     returns false if you would want to append a '\' to the end
  34. of FName before added a file name.
  35. }
  36. var
  37.     l : Short;
  38. begin
  39.     l := strlen(FName);
  40.     if l = 0 then
  41.     DirEnded := True;
  42.     DirEnded := (FName[l - 1] = ':') or (FName[l - 1] = '/');
  43. end;
  44.  
  45. Procedure CheckFile(FileInfo : FileInfoBlockPtr);
  46. {
  47.     Displays whatever information you want from the FileInfoBlock
  48. }
  49. var
  50.     l : Short;
  51.     NameBuffer : Array [0..255] of Char;
  52.     Name : String;
  53.     F    : File of Char;
  54. begin
  55.     Name := Adr(NameBuffer);
  56.  
  57.     with FileInfo^ do begin
  58.     StrCpy(Name, FullPath);
  59.     l := strlen(FullPath);
  60.     if not DirEnded(FullPath) then
  61.         StrCat(Name, "/");
  62.     StrCat(Name, String(Adr(fib_FileName)));
  63.     Write('\r\e[KChecking ', Name);
  64.     if reopen(Name, F, 10000) then begin
  65.         while not eof(F) do begin
  66.         Get(F);
  67.         if IOResult <> 0 then begin
  68.             Writeln('\e[KError Reading ', Name);
  69.             Inc(Errors);
  70.             Close(F);
  71.             Return;
  72.         end;
  73.         end;
  74.         Close(F);
  75.         if IOResult <> 0 then begin
  76.         Writeln('\e[KError Closing ', Name);
  77.         Inc(Errors);
  78.         end;
  79.     end;
  80.     end;
  81. end;
  82.  
  83. Procedure UpCase(str : String);
  84. {
  85.     Converts a string to uppercase
  86. }
  87. var
  88.     i : Integer;
  89. begin
  90.     i := 0;
  91.     while str[i] <> '\0' do begin
  92.     str[i] := toupper(str[i]);
  93.     i := Succ(i);
  94.     end;
  95. end;
  96.  
  97. Procedure SearchDirectory(DirName : String);
  98. {
  99.     The big routine.  Runs through the named directory, printing
  100. file names that match the global variable FileName, and making a list
  101. of directories.  When it has finished looking through the directories,
  102. it calls itself recursively to print its subdirectories.
  103. }
  104. var
  105.     FL        : FileLock;
  106.     FB        : FileInfoBlockPtr;
  107.     Stay    : Boolean;
  108.     LastPos    : Short;
  109.     FirstDir,
  110.     TempDir    : DirRecPtr;
  111.     DOSError    : Integer;
  112. begin
  113.     if CheckBreak then
  114.     return;
  115.     LastPos := StrLen(FullPath);
  116.     FirstDir := Nil;
  117.     if not DirEnded(FullPath) then
  118.     strcat(FullPath, "/");
  119.     strcat(FullPath, DirName);
  120.     FL := Lock(FullPath, ACCESS_READ);
  121.     if FL = Nil then begin
  122.     FullPath[LastPos] := '\0';
  123.     return;
  124.     end;
  125.     New(FB);    { Since New() uses AllocMem, FB is longword aligned }
  126.     if not Examine(FL, FB) then begin
  127.     Unlock(FL);
  128.     FullPath[LastPos] := '\0';
  129.     return;
  130.     end;
  131.     if FB^.fib_DirEntryType < 0 then begin { means it's a file, not a dir }
  132.     Unlock(FL);
  133.     FullPath[LastPos] := '\0';
  134.     return;
  135.     end;
  136.     repeat
  137.     Stay := ExNext(FL, FB);
  138.     if Stay then begin
  139.         with FB^ do begin
  140.         if fib_DirEntryType < 0 then begin { file }
  141.             StrCpy(TestName, Adr(fib_FileName));
  142.             UpCase(TestName);
  143.             CheckFile(FB);
  144.         end else begin            { a dir }
  145.             new(TempDir);        { add it to the list }
  146.             with TempDir^ do begin
  147.             strcpy(Adr(Name), Adr(fib_FileName));
  148.             Previous := FirstDir;
  149.             end;
  150.             FirstDir := TempDir;
  151.         end;
  152.         end;
  153.     end else begin
  154.         DOSError := IoErr;    { expect Error_No_More_Entries - not an error }
  155.         if DOSError <> ERROR_NO_MORE_ENTRIES then
  156.         Writeln('DOS Error number ', DOSError);
  157.     end;
  158.     if CheckBreak then begin        { user pressed Ctrl-C? }
  159.         while FirstDir <> Nil do begin
  160.         TempDir := FirstDir^.Previous;    { don't go through subs }
  161.         Dispose(FirstDir);
  162.         FirstDir := TempDir;
  163.         end;
  164.         Stay := False;
  165.     end;
  166.     until not Stay;
  167.     Unlock(FL);
  168.     while FirstDir <> Nil do begin        { print sub-directories }
  169.     SearchDirectory(Adr(FirstDir^.Name));
  170.     TempDir := FirstDir^.Previous;
  171.     Dispose(FirstDir);
  172.     FirstDir := TempDir;
  173.     end;
  174.     FullPath[LastPos] := '\0';            { restore global path name }
  175. end;
  176.  
  177. Procedure Usage;
  178. begin
  179.     Writeln('Usage: CheckDisk BaseDirectory');
  180.     Exit(20);
  181. end;
  182.  
  183. begin
  184.     Directory := AllocString(128);
  185.     TestName := AllocString(110);
  186.     FullPath := AllocString(300);    { allocate plenty of space }
  187.     FullPath[0] := '\0';
  188.     GetParam(1, Directory);
  189.     if Directory^ = '\0' then
  190.     Usage;
  191.     Errors := 0;
  192.     SearchDirectory(Directory);
  193.     WriteLn('\r\e[KThere were ', Errors, ' errors.');
  194. end.
  195.